package com.hello.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hello.common.log.annotation.Log;
import com.hello.common.log.enums.BusinessType;
import com.hello.common.security.annotation.RequiresPermissions;
import com.hello.system.domain.RentalMemberInfo;
import com.hello.system.service.IRentalMemberInfoService;
import com.hello.common.core.web.controller.BaseController;
import com.hello.common.core.web.domain.AjaxResult;
import com.hello.common.core.utils.poi.ExcelUtil;
import com.hello.common.core.web.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 会员信息Controller
 * 
 * @author hello
 * @date 2025-07-20
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/member/info")
public class RentalMemberInfoController extends BaseController
{
    private final IRentalMemberInfoService rentalMemberInfoService;

    /**
     * 查询会员信息列表
     */
    @RequiresPermissions("system:member:list")
    @GetMapping("/list")
    public TableDataInfo list(RentalMemberInfo rentalMemberInfo)
    {
        log.info("======== 查询会员信息列表请求 ========");
        log.info("请求参数: {}", rentalMemberInfo);
        
        startPage();
        List<RentalMemberInfo> list = rentalMemberInfoService.selectRentalMemberInfoList(rentalMemberInfo);
        TableDataInfo result = getDataTable(list);
        
        log.info("查询结果: 总记录数={}, 当前页数据量={}", result.getTotal(), list.size());
        log.info("======== 查询会员信息列表完成 ========");
        
        return result;
    }

    /**
     * 导出会员信息列表
     */
    @RequiresPermissions("system:member:export")
    @Log(title = "会员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RentalMemberInfo rentalMemberInfo)
    {
        log.info("======== 导出会员信息数据请求 ========");
        log.info("导出条件: {}", rentalMemberInfo);
        
        List<RentalMemberInfo> list = rentalMemberInfoService.selectRentalMemberInfoList(rentalMemberInfo);
        log.info("导出数据量: {} 条记录", list.size());
        
        ExcelUtil<RentalMemberInfo> util = new ExcelUtil<RentalMemberInfo>(RentalMemberInfo.class);
        util.exportExcel(response, list, "会员信息数据");
        
        log.info("======== 会员信息数据导出完成 ========");
    }

    /**
     * 获取会员信息详细信息
     */
    @RequiresPermissions("system:member:query")
    @GetMapping(value = "/{memberId}")
    public AjaxResult getInfo(@PathVariable("memberId") Long memberId)
    {
        log.info("======== 获取会员信息详情请求 ========");
        log.info("会员ID: {}", memberId);
        
        RentalMemberInfo memberInfo = rentalMemberInfoService.selectRentalMemberInfoByMemberId(memberId);
        log.info("查询结果: {}", memberInfo != null ? "找到会员信息" : "未找到对应会员");
        
        log.info("======== 获取会员信息详情完成 ========");
        return success(memberInfo);
    }

    /**
     * 新增会员信息
     */
    @RequiresPermissions("system:member:add")
    @Log(title = "会员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RentalMemberInfo rentalMemberInfo)
    {
        log.info("======== 新增会员信息请求 ========");
        log.info("新增数据: {}", rentalMemberInfo);
        
        int result = rentalMemberInfoService.insertRentalMemberInfo(rentalMemberInfo);
        log.info("新增结果: {}", result > 0 ? "成功" : "失败");
        
        log.info("======== 新增会员信息完成 ========");
        return toAjax(result);
    }

    /**
     * 修改会员信息
     */
    @RequiresPermissions("system:member:edit")
    @Log(title = "会员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RentalMemberInfo rentalMemberInfo)
    {
        log.info("======== 修改会员信息请求 ========");
        log.info("修改数据: {}", rentalMemberInfo);
        
        int result = rentalMemberInfoService.updateRentalMemberInfo(rentalMemberInfo);
        log.info("修改结果: {}", result > 0 ? "成功" : "失败");
        
        log.info("======== 修改会员信息完成 ========");
        return toAjax(result);
    }

    /**
     * 删除会员信息
     */
    @RequiresPermissions("system:member:remove")
    @Log(title = "会员信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{memberIds}")
    public AjaxResult remove(@PathVariable Long[] memberIds)
    {
        log.info("======== 删除会员信息请求 ========");
        log.info("删除会员ID列表: {}", java.util.Arrays.toString(memberIds));
        log.info("删除数量: {} 个会员记录", memberIds.length);
        
        int result = rentalMemberInfoService.deleteRentalMemberInfoByMemberIds(memberIds);
        log.info("删除结果: {}", result > 0 ? "成功" : "失败");
        
        log.info("======== 删除会员信息完成 ========");
        return toAjax(result);
    }
}
